package main

import (
	"file-rename/service"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	//service.RenameForDirPath("/Cache/00", "S01SE", 3, 1)
	webServer()
}

func webServer() {
	http.Handle("/static/", service.StaticHandler())
	http.HandleFunc("/favicon.ico", service.Favicon)
	http.HandleFunc("/", service.Index)

	var port = 8080
	server := &http.Server{Addr: ":" + fmt.Sprint(port)}
	go func() {
		err := server.ListenAndServe()
		if err != nil {
			log.Fatalln("http serve failed", err)
		}
	}()
	log.Println("listening and serving HTTP on", port)
	waitForShutdown(server)
}

func waitForShutdown(server *http.Server) {
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
	<-sigChan
	err := server.Shutdown(nil)
	if err != nil {
		log.Println("server shutdown failed", err)
		return
	}
	log.Println("server shutdown successful")
}
